package start.spring.basic.log;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONObject;

/**
 *
 * ****************************************************************************
 * 切面公共工具类
 *
 * @author(作者)：xuyongyun	
 * @author(复审人)：	Administrator 2018年6月29日
 * @version(版本)1.0
 * @since 1.0
 ******************************************************************************
 */
public class AspectUtil {

	/**
	 * 非@around通知
	 *	
	 * @param joinPoint
	 * @return
	 * @since           1.0
	 */
	public static JSONObject getJoinPointInfo(JoinPoint joinPoint) {
		JSONObject appInfo = new JSONObject();
		//类名
		String className = joinPoint.getTarget().getClass().getName();
		//方法名
		String methodName = joinPoint.getSignature().getName();
		
		appInfo.put("className", className);
		appInfo.put("methodName", methodName);
		Object[] requestArgs = joinPoint.getArgs();
		List<Object> argList = new ArrayList<>();
		for(Object arg:requestArgs) {
			if((!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse) 
					&& !(arg instanceof BindingResult)) && !(arg instanceof MultipartHttpServletRequest)) {
				argList.add(arg);
			}
		}
		appInfo.put("requestArgs", argList);
		return appInfo;
	}
	
	/**
	 * @around通知
	 *	
	 * @param joinPoint
	 * @return
	 * @since           1.0
	 */
	public static JSONObject getJoinPointInfo(ProceedingJoinPoint pjp) {
		JSONObject appInfo = new JSONObject();
		//类名
		String className = pjp.getTarget().getClass().getName();
		//方法名
		String methodName = pjp.getSignature().getName();
		
		appInfo.put("className", className);
		appInfo.put("methodName", methodName);
		Object[] requestArgs = pjp.getArgs();
		List<Object> argList = new ArrayList<>();
		for(Object arg:requestArgs) {
			if((!(arg instanceof HttpServletRequest) && !(arg instanceof HttpServletResponse)
					&& !(arg instanceof BindingResult) && !(arg instanceof MultipartHttpServletRequest))) {
				argList.add(arg);
			}
		}
		appInfo.put("requestArgs", argList);
		return appInfo;
	}
}
